package com.tulele.vtool.service.impl;

import com.tulele.vtool.mapper.AdvertInfoMapper;
import com.tulele.vtool.model.AdvertInfo;
import com.tulele.vtool.model.AdvertTagInfo;
import com.tulele.vtool.model.ArticleAdvertInfo;
import com.tulele.vtool.service.AdvertInfoService;
import com.tulele.vtool.utils.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdvertInfoServiceImpl implements AdvertInfoService {

    @Autowired
    private AdvertInfoMapper advertInfoMapper;
    @Override
    public void addAdvertByUserId(AdvertInfo advertInfo) {
        // id设置为null 防止恶意sql注入
        advertInfo.setId(null);
        advertInfo.setAdvertStatus(1);
        advertInfo.setCreateTime(DateUtil.getCurDate());
        advertInfo.setModifyTime(advertInfo.getCreateTime());
        this.advertInfoMapper.addAdvertByUserId(advertInfo);
    }

    @Override
    public List<AdvertInfo> selectAdvertByUserIdAndAdvertType(AdvertInfo advertInfo) {
        return this.advertInfoMapper.selectAdvertByUserIdAndAdvertType(advertInfo);
    }

    @Override
    public void deleteAdvertByAdvertId(Integer advertId) {
        this.advertInfoMapper.deleteAdvertByAdvertId(advertId);
        // 查询改广告是否被视频使用 如被使用将解除关联
        ArticleAdvertInfo articleAdvertInfo = new ArticleAdvertInfo();
        articleAdvertInfo.setAdvertId(advertId);
        ArticleAdvertInfo articleAdvert = this.advertInfoMapper.selectVideoAdvert(articleAdvertInfo);
        if (null != articleAdvert){
             this.advertInfoMapper.delVideoAdvert(articleAdvert);
        }
    }

    @Override
    public void updateAdvertByAdvertId(AdvertInfo advertInfo) {
        advertInfo.setModifyTime(DateUtil.getCurDate());
        this.advertInfoMapper.updateAdvertByAdvertId(advertInfo);
    }

    @Override
    public List<AdvertTagInfo> selectAdvertTagByTagTypeAndTagId(AdvertTagInfo advertTagInfo) {
        return this.advertInfoMapper.selectAdvertTagByTagTypeAndTagId(advertTagInfo);
    }

    @Override
    public void addAdvertTag(AdvertTagInfo advertTagInfo) {
        advertTagInfo.setId(null);
        advertTagInfo.setCreateTime(DateUtil.getCurDate());
        advertTagInfo.setModifyTime(advertTagInfo.getCreateTime());
        this.advertInfoMapper.addAdvertTag(advertTagInfo);
    }

    @Override
    public void deleteAdvertTagByTagId(AdvertTagInfo advertTagInfo) {
        this.advertInfoMapper.deleteAdvertTagByTagId(advertTagInfo.getId());
    }

    @Override
    public List<AdvertInfo> selectAdvertByLikeAdvert(AdvertInfo advertInfo) {
        return this.advertInfoMapper.selectAdvertByLikeAdvert(advertInfo);
    }

    @Override
    public void addVideoAdvert(ArticleAdvertInfo articleAdvertInfo) {
        // 先查询改视频是否加入了广告 如果有 将删除
        ArticleAdvertInfo articleAdvert = new ArticleAdvertInfo();
        articleAdvert.setArticleId(articleAdvertInfo.getArticleId());
        articleAdvert.setUserId(articleAdvertInfo.getUserId());
        ArticleAdvertInfo returnArticleAdvert = this.advertInfoMapper.selectVideoAdvert(articleAdvert);
        if (null != returnArticleAdvert){
            this.advertInfoMapper.delVideoAdvert(returnArticleAdvert);
        }
        // 添加广告
        articleAdvertInfo.setId(null);
        articleAdvertInfo.setCreateTime(DateUtil.getCurDate());
        articleAdvertInfo.setModifyTime(articleAdvertInfo.getCreateTime());
        this.advertInfoMapper.addVideoAdvert(articleAdvertInfo);
    }

    @Override
    public void delVideoAdvert(ArticleAdvertInfo articleAdvertInfo) {
        this.advertInfoMapper.delVideoAdvert(articleAdvertInfo);
    }

    @Override
    public AdvertInfo selectVideoAdvert(ArticleAdvertInfo articleAdvertInfo) {
        // 查询出视频关联的广告
        ArticleAdvertInfo articleAdvert = this.advertInfoMapper.selectVideoAdvert(articleAdvertInfo);
        AdvertInfo advertInfo = new AdvertInfo();
        if(articleAdvert!=null) {
            advertInfo.setId(articleAdvert.getAdvertId());
        }
        // 通过广告id查询广告
        return this.selectAdvertByAdvertId(advertInfo);
    }

    @Override
    public AdvertInfo selectAdvertByAdvertId(AdvertInfo advertInfo) {
        return this.advertInfoMapper.selectAdvertByAdvertId(advertInfo);
    }

    @Override
    public void updateVideoAdvert(ArticleAdvertInfo articleAdvertInfo) {
        //删除广告关联
        ArticleAdvertInfo articleAdvert = new ArticleAdvertInfo();
        articleAdvert.setArticleId(articleAdvertInfo.getArticleId());
        this.delVideoAdvert(articleAdvert);
        // 添加新关联
        this.addVideoAdvert(articleAdvertInfo);
    }
}
